#include <mqueue.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <iostream>
using namespace std;

#define FILE_MODE S_IRUSR|S_IWUSR

struct mq_attr attr;

int main(int argc, char** argv){
    int c, flags;
    mqd_t mqd;
    ssize_t n;
    unsigned int  prio;
    
    void* buff;
    struct mq_attr attr;
    
    flags = O_RDWR|O_CREAT;

    while( ( c = getopt(argc, argv, "n")) != -1 ){
        switch(c){
        case 'n':
            flags |= O_NONBLOCK;
            break;
        }
    }
            
    if( optind != argc -1){
        printf("usage: mqcreate [-n]  <name>\n");
        return  -1;
    }
    
    mqd = mq_open(argv[optind], flags);
    mq_getattr(mqd, &attr);
    
    buff = malloc(attr.mq_msgsize);
    n = mq_receive(mqd, (char*)buff, attr.mq_msgsize, &prio);
    printf("read %ld bytes, priority = %u\n", (long) n, prio);
    exit(0);
}

